package com.click.app.model;

import com.click.app.Functions;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * @author Joel Chuca Condo
 * @date 22/04/2009
 */
@Entity
@Table(name = "package_details")
public class PackageDetail extends BaseObject {

    private static final long serialVersionUID = 1L;
    
    
    private Long id;
    private String code;
    private String packageName;
    private Double quantity;
    private Double stock;
    private StorageMaterial storageMaterial;
    private PurchaseDetail purchaseDetail;
    private Boolean verified;
    private Double realQuantity;
    private Set<AsignationCut> asignationCuts;
    private Double quantityCurrentUsed;

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column(name="id_package_detail", unique=true, nullable=false)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "package_name", length = 45)
    public String getPackageName() {
        return packageName;
    }

    public void setPackageName(String packageName) {
        this.packageName = packageName;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_purchase_detail", nullable = false)
    public PurchaseDetail getPurchaseDetail() {
        return purchaseDetail;
    }

    public void setPurchaseDetail(PurchaseDetail purchaseDetail) {
        this.purchaseDetail = purchaseDetail;
    }

    @Column(name = "quantity", precision = 6, scale = 0)
    public Double getQuantity() {
        return Functions.getDouble3Decimal(quantity);
    }

    public void setQuantity(Double quantity) {
        this.quantity = Functions.getDouble3Decimal(quantity);
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "id_storage_material", nullable = false)
    public StorageMaterial getStorageMaterial() {
        return storageMaterial;
    }

    public void setStorageMaterial(StorageMaterial storageMaterial) {
        this.storageMaterial = storageMaterial;
    }

    @Column(name = "stock", precision = 6, scale = 0)
    public Double getStock() {
        return Functions.getDouble3Decimal(stock);
    }

    public void setStock(Double stock) {
        this.stock = Functions.getDouble3Decimal(stock);
    }

    @Column(name = "code", length = 45)
    public String getCode() {
        if (code==null){

            System.out.println(this.getPurchaseDetail());
        }
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public Boolean getVerified() {
        return verified;
    }

    public void setVerified(Boolean verified) {
        this.verified = verified;
    }

    @Column(name = "real_quantity", precision = 6, scale = 0)
    public Double getRealQuantity() {
        return Functions.getDouble3Decimal(realQuantity);
    }

    public void setRealQuantity(Double realQuantity) {
        this.realQuantity = Functions.getDouble3Decimal(realQuantity);
    }

    @OneToMany(mappedBy = "packageDetail")
    public Set<AsignationCut> getAsignationCuts() {
        return asignationCuts;
    }

    public void setAsignationCuts(Set<AsignationCut> asignationCuts) {
        this.asignationCuts = asignationCuts;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof PackageDetail)) {
            return false;
        }
        PackageDetail other = (PackageDetail) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, ToStringStyle.SIMPLE_STYLE)
                .append(this.id).append(this.code)
                .append(this.packageName).append(this.quantity).append(this.stock)
                .append(this.verified).append(this.realQuantity).append(this.quantityCurrentUsed).toString();

    }

    @Column(name = "quantity_current_used", precision = 6, scale = 0)
    public Double getQuantityCurrentUsed() {
        return Functions.getDouble3Decimal(quantityCurrentUsed);
    }

    public void setQuantityCurrentUsed(Double quantityCurrentUsed) {
        this.quantityCurrentUsed = Functions.getDouble3Decimal(quantityCurrentUsed);
    }

}
